package testvolatile;

/**
 * @Package: testvolatile
 * @ClassName: VolatileObjectTest
 * @Author: lujieni
 * @Description:
 * @Date: 2021-04-07 11:06
 * @Version: 1.0
 */
public class VolatileObjectTestAgainAgain implements Runnable {

    private  ObjectA a;
    public VolatileObjectTestAgainAgain(ObjectA a) {
        this.a = a;
    }

    public ObjectA getA() {
        return a;
    }

    public void setA(ObjectA a) {
        this.a = a;
    }

    @Override
    public void run() {
        long i = 0;
        while (a != null) {
            i++;
            if(a.flag){

            }
        }
        System.out.println("stop My Thread " + i);
    }

    public void stop() {
        a.setFlag(false);
        a = null;
    }



    public static void main(String[] args) throws Exception {

        // 如果启动的时候加上-server 参数则会 输出 Java HotSpot(TM) Server VM
        System.out.println(System.getProperty("java.vm.name"));

        VolatileObjectTestAgainAgain test = new VolatileObjectTestAgainAgain(new ObjectA());
        new Thread(test).start();

        Thread.sleep(1000);
        test.stop();
        Thread.sleep(1000);
        //System.out.println("Main Thread " + test.getA().isFlag());
    }

    static class ObjectA {
        private volatile boolean flag = true;

        public boolean isFlag() {
            return flag;
        }

        public void setFlag(boolean flag) {
            this.flag = flag;
        }
    }
}